package com.xaomn.common.config;

import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @author xaoMn
 * @title: ShiroConfig
 * @projectName yjs
 * @description: TODO
 * @date 日期：2022/1/25 时间：23:16
 */
@Configuration
public class ShiroConfig {

    /**
     * 　　* @description: SecurityManager 流程控制
     * 　　* @param ${tags}
     * 　　* @return ${return_type}
     * 　　* @throws
     * 　　* @author xaoMn
     * 　　* @date 2022/1/25 23:32
     *
     */
    @Bean
    public DefaultWebSecurityManager mySecurityManager(Realm yjsRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(yjsRealm);
        return securityManager;
    }

    /**
     * 　　* @description: ShrioFilterFactoryBean 请求过滤器
     * 　　* @param ${tags}
     * 　　* @return ${return_type}
     * 　　* @throws
     * 　　* @author xaoMn
     * 　　* @date 2022/1/25 23:36
     *
     */
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager mySecurityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(mySecurityManager);
        // 配置路径过滤器
        HashMap<String, String> filterMap = new HashMap<>();
        // 配置ant路径 key是ant路径，value是默认过滤器:DefaultFilter中的key
        filterMap.put("/mobile/**","authc");
        filterMap.put("/salary/**","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        return shiroFilterFactoryBean;
    }
}
